package com.gsww.vue.controller.sys;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.gsww.vue.controller.BaseController;
import com.gsww.vue.entity.sys.SysAccount;
import com.gsww.vue.entity.sys.SysDepartment;
import com.gsww.vue.entity.sys.SysRole;
import com.gsww.vue.service.sys.SysAccountService;
import com.gsww.vue.service.sys.SysDepartmentService;
import com.gsww.vue.service.sys.SysRoleService;
import com.gsww.vue.utils.Md5;
import com.gsww.vue.utils.StringHelper;
import com.gsww.vue.utils.TimeHelper;
/**
 * <p>Copyright: Copyright (c) 2014</p>
 * <p>公司名称 : 中国电信甘肃万维公司</p>
 * <p>项目名称 : jup-web</p>
 * <p>创建时间 : 2014-6-08 下午10:55:12</p>
 * <p>类描述 :   账号管理模块控制器    </p>
 *
 * @version 3.0.0
 * @author <a href=" ">zhangtb</a>
 */
@RestController
@RequestMapping(value = "/sys")
public class SysAccountController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(SysAccountController.class);
	@Autowired
	private SysAccountService sysAccountService;
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysDepartmentService sysDepartmentService;
	private Map<String,String> resMap=new HashMap<String,String>();

	/**
	 * 获取用户列表
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/accountList",method = RequestMethod.GET)
	public Map<String,Object> accountList(HttpServletRequest request,HttpServletResponse response) {
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		String userName = request.getParameter("userName");
		Map<String,Object> resMap = new HashMap<String,Object>();
		try{
			Map<String,Object> map = sysAccountService.getAccountList(userName, null,pageNo,pageSize);
//			Map<String,String> userRolemap=new HashMap<String,String>();
			List<Map<String,Object>> list = (List<Map<String, Object>>) map.get("result");
			//查询用户的角色
			for (Map<String,Object> account : list) {
				List<SysRole> roleList=new ArrayList<SysRole>();
				roleList=sysAccountService.findAccountRoleList(account.get("USER_ACCT_ID").toString());
				String userRoleName = "";
				for (SysRole sysRole : roleList) {
					userRoleName += sysRole.getRoleName()+",";
				}
				if(StringUtils.isNotBlank(userRoleName)){
					userRoleName=userRoleName.substring(0, userRoleName.length()-1);
				}
				account.put("ROLE_NAME", userRoleName);
//				if(StringUtils.isNotBlank(userRoleName)){
//					userRoleName=userRoleName.substring(0, userRoleName.length()-1);
//					userRolemap.put(account.get("USER_ACCT_ID").toString(), userRoleName);
//				}else{
//					userRolemap.put("0", "");
//				}
			}
			
			//map.put("userRolemap", userRolemap);
			resMap.put("data", map);
			resMap.put("success", true);
		}catch(Exception ex){
			resMap.put("success", false);
			ex.printStackTrace();
		}
		return resMap;
	}
	
	@RequestMapping(value = "/accountEdit", method = RequestMethod.GET)
	public Map<String,Object> accountEdit(HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		String userAcctId = request.getParameter("userAcctId");
		try {
			SysAccount sysAccount = null;
			if(StringHelper.isNotBlack(userAcctId)){
				sysAccount = sysAccountService.findByKey(userAcctId);
				List<SysRole> roleList=new ArrayList<SysRole>();
				roleList = sysAccountService.findAccountRoleList(sysAccount.getUserAcctId());
				List<String> roleIds = new ArrayList<String>();
				for (SysRole sysRole : roleList) {
					roleIds.add(sysRole.getRoleId());
				}
				resMap.put("userRoleList", roleList);
				resMap.put("userRoleId", roleIds);
			}else{
				sysAccount = new SysAccount();
				SysDepartment dep = new SysDepartment();
				sysAccount.setSysDepartment(dep);
			}
			resMap.put("sysAccount", sysAccount);
			List<SysRole> list=new ArrayList<SysRole>();
			list = sysRoleService.findRoleList();
			resMap.put("sysRoleList", list);
			resMap.put("success", true);
		} catch (Exception e) {
			resMap.put("success", false);
			e.printStackTrace();
		}
		return resMap;
	}
	/**
	 * 保存用户信息
	 */
	@RequestMapping(value = "/accountSave", method = RequestMethod.POST)
	//@RequestParam String  userAcctId,@RequestParam String  deptId, @RequestParam SysAccount sysAccount
	//@RequestBody Map<String,Object> params
	public Map<String,Object> accountSave(@RequestBody SysAccount  sysAccount,HttpServletRequest request,HttpServletResponse response)  throws Exception {
		String userRoles = request.getParameter("userRoles");
		String  deptId = sysAccount.getSysDepartment().getDeptId();
		System.out.println("--"+userRoles);
		Map<String,Object> resMap = new HashMap<String,Object>();
		try {
			if(!StringHelper.isNotBlack(sysAccount.getUserState())){
				sysAccount.setUserState("1");
			}
			if(!StringHelper.isNotBlack(sysAccount.getLoginPassword())){
				Md5 m = new Md5();
				sysAccount.setLoginPassword(m.getMD5ofStr("111111"));
			}
			if(StringUtils.isBlank(sysAccount.getUserSex())){
				sysAccount.setUserSex("2");
			}
			if(StringUtils.isBlank(sysAccount.getUserAcctId())) {
				sysAccount.setCreateTime(TimeHelper.getCurrentTime());
			}
			SysDepartment sysDepartment=null;
			sysDepartment = sysDepartmentService.findByKey(deptId);
			sysAccount.setSysDepartment(sysDepartment);
			//1、先保存用户
			
			sysAccount = sysAccountService.save(sysAccount);
			    
			//2、保存用户角色关系表
			sysAccountService.deleteAccountRole(sysAccount);
			//String userRoles=request.getParameter("userRole");
			if(StringUtils.isNotBlank(userRoles)){
				String[] roles=userRoles.split(",");
				for (String roleId : roles) {
					
					sysAccountService.saveUserRole(sysAccount.getUserAcctId(), roleId);
				}
			}
			//System.out.println("Saveuser:"+sysAccount.getUserAcctId());
			resMap.put("success",true);
		} catch (Exception e) {
			resMap.put("success",false);
			e.printStackTrace();
		} 
		return resMap;
	}
	@RequestMapping(value = "/deleteAccount",method = RequestMethod.GET)
	public Map<String,Object> deleteAccount(HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		String userId = request.getParameter("user_acct_id");
		try {
			SysAccount account = sysAccountService.findByKey(userId);
			account.setUserState("0");
			sysAccountService.save(account);
			resMap.put("success",true);
			
		} catch (Exception e) {
			resMap.put("success",false);
			e.printStackTrace();
		}
		return resMap;
	}
}
